import _ from "lodash-es";
export const setDatalist = (dataString: string) => {
  // 假设每一行都是格式化的，我们可以使用正则表达式来分割行
  const lines = _.compact(dataString.split("\n"));
  console.log(lines);
  // 创建一个空数组用于存放单词对象
  const words = [];
  lines.map((item) => {
    let chineseList = [
      "窗帘",
      "架子",
      "厕所",
      "衣柜",
      "毛毯/地毯",
      "手扶椅",
      "炊具",
      "台灯",
      "镜子",
      "淋浴",
      "某个地方",
      "公寓",
      "相当地",
    ];
    if (item.trim()) {
      const [word, chinese, phonetic] = item.split("	");
      // 创建一个新的单词对象，并将其添加到words数组中
      words.push({
        word: word.trim(),
        chinese: chinese.trim(),
        phonetic:
          phonetic && phonetic.trim() ? phonetic && phonetic.trim() : "",
        choices: getRandomChoices(chineseList, chinese.trim(), 3),
      });
    }
  });
  return words;
};
function getRandomChoices(array: any[], correctItem: string, count: number) {
  let choices = [correctItem];
  while (choices.length < count) {
    let randomItem = array[Math.floor(Math.random() * array.length)];
    if (!choices.includes(randomItem) && randomItem !== correctItem) {
      choices.push(randomItem);
    }
  }
  return choices.sort(() => Math.random() - 0.5); // 打乱选项顺序
}
